import 'package:flutter/widgets.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/gaps.dart';
import 'package:yt_devops/components/yt_expandable_tree/yt_expandable_tree.dart';
import 'package:yt_devops/components/yt_multi_column_picker/model/yt_multi_column_model.dart';
import 'package:yt_devops/pages/site/site_observer_detail/views/observer_point_no_data.dart';
import 'package:yt_devops/utils/func_utils.dart';

import 'yt_multi_column_picker_controller.dart';

class YtMultiColumnPicker<T> extends GetView<YtMultiColumnPickerController> {
  final MultiColumnModel<T> data;

  final List<int> defaultFocusedIndexes;

  final Function(int column, int row, MultiColumnModel)? onModelTap;

  const YtMultiColumnPicker(this.data,
      {this.defaultFocusedIndexes = const [], this.onModelTap, Key? key})
      : super(key: key);

  @override
  get controller => Get.put(YtMultiColumnPickerController(data,
      defaultFocusedIndexes: defaultFocusedIndexes));

  @override
  Widget build(BuildContext context) {
    return GetBuilder<YtMultiColumnPickerController>(
      initState: (state) => controller.initData(),
      didChangeDependencies: (state) {
        controller.data = data;
        controller.defaultFocusedIndexes = defaultFocusedIndexes;

        controller.initData();
      },
      builder: (controller) => getWidget(),
    );
  }

  Widget getWidget() {
    List<Widget> widgets = [];
    if (!FuncUtils.isListEmpty(controller.firstList)) {
      widgets.add(
        SizedBox(
          width: 140.w,
          child: YtExpandableTree(
            treeData: controller.firstList,
            isSearch: false,
            checkedValues: [],
            backgroudColor: const Color(0xFFF6F7FB),
            showLine: false,
            selectedValues: controller.firstSelectedValues,
            onSelect: (index, newVal) {
              controller.onFirstSelect(index);
              onModelTap?.call(0, index, newVal as MultiColumnModel<T>);
            },
          ),
        ),
      );
    }

    if (FuncUtils.isListEmpty(controller.secondList)) {
      widgets.add(
        Expanded(
          child: ObserverPointNoData(
            topPosition: 60,
          ),
        ),
      );
    } else {
      widgets.add(
        Expanded(
          child: YtExpandableTree(
            isSearch: false,
            multiple: false,
            showLine: false,
            treeData: controller.secondList,
            checkedValues: controller.checkedValues,
            selectedValues: controller.checkedValues,
            onSelect: (index, newVal) {
              if (index == 0) {
                controller.onSecondSelect(index, newVal as MultiColumnModel<T>);
                onModelTap?.call(1, index, newVal);
              }
            },
            onCheck: (index, newVal) {
              controller.onSecondSelect(index, newVal as MultiColumnModel<T>);
              onModelTap?.call(1, index, newVal);
            },
          ),
        ),
      );
    }
    widgets.add(Gaps.hGap16);

    return Row(
      children: widgets,
    );
  }
}
